<?php
/**设置银行密码*/
class ChangeBankPassword extends APIBase{

	public $tag = "ChangeBankPassword";
	public $isLogin = true;

	public function before() {
		return true;
	}

	public function logic() {
		
		$ret = $this->data_redis->hMGet("hu:{$this->uid}", array('is_set_bank_password','bank_password', 'bank_salt') );
		$password_reg = '/[a-zA-Z0-9]{4,10}/';
		

		if ( !isset($this->param['new_password']) )
		{
			$this->returnError(401, "请输入新密码");
		}
		else if (!preg_match($password_reg, $this->param['new_password']))
		{
			$this->returnError(402, "密码长度要求4-10位，允许英文、数字。请重新填写");
		}
		
		//初始使用，设置密码
		if ( isset($this->param['first_use_bank']) )
		{
			if ( $ret['is_set_bank_password'] != 0 )
			{
				//已经设置过密码了，不是初次设置
				$this->returnError(403, "已经设置过密码了");
			}			
			else
			{
				//新密码设置成功
				$salt = Game::$salt_password;
				$ret['bank_password'] = md5($this->param['new_password'] . $salt);
				$ret['is_set_bank_password'] = 1;
				$ret['bank_salt'] = $salt;
			}
		}
		else //修改密码
		{
			if ( $ret['is_set_bank_password'] == 0 )
			{
				//还没有设置过密码
				$this->returnError(404, "还没设置初始密码，不能修改密码");
			}
			
			$old_password = md5($this->param['old_password'].$ret['bank_salt']);
			
			if ($old_password != $ret['bank_password'])
			{
				//旧密码不对
				$this->returnError(405, "旧密码错误");
			}
			else 
			{
				//新密码设置成功
				$ret['bank_password'] = md5($this->param['new_password'] . $ret['bank_salt']);
			}
		}
		//http://192.168.0.61:9090/zjhapi/api.php?action=ChangeBankPassword&ver=1.5.1&uid=4871&skey=519c09e04adb29f4ae62a9eef528e108&mainID%20=%2016777218&AssetID%20=%205&param={%22old_password%22:%22123456%22,%22new_password%22:%22112233%22}

		$this->data_redis->hMset("hu:{$this->uid}", $ret);
		
		$result = $this->data_redis->hMget("hu:{$this->uid}", array('is_set_bank_password', 'bank_password'));
		$this->returnData($result, 0, "密码设置成功");

	}

	public function after() {
		$this->deinitDataRedis();
	}
}